public class test4 {
    public int maxProfit1(int[] prices) {
        // 动态规划 --> 可以进行多次交易
        int n = prices.length;
        int[] dp1 = new int[n];
        int[] dp2 = new int[n];
        dp1[0] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp1[i] = Math.max(dp1[i-1],dp2[i-1]-prices[i]);// 买入点
            dp2[i] = Math.max(dp2[i-1],dp1[i-1]+prices[i]);// 卖出点
        }
        return dp2[n-1];
    }
    public int maxProfit(int[] prices) {
        // 只要开始上升就买,开始下降就卖
        // 双指针
        int sum = 0;
        int n = prices.length;
        for (int i = 0; i < n; i++) {
            int j = i;
            while (j+1<n&&prices[j]<prices[j+1]) j++;
            sum += (prices[j]-prices[i]);
            i=j;
        }
        return sum;
    }
}
